package com.itheima.controller;

import com.itheima.R;
import com.itheima.dto.CateDTO;
import com.itheima.service.YddCateService;
import com.itheima.vo.CateVO2;
import com.itheima.vo.PageInfoVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ydd_cate")
@RequiredArgsConstructor
public class CateController {

    private final YddCateService yddCateService;

    /**
     * 分类列表-分页查询
     *
     * @param catename
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public R<PageInfoVO<CateVO2>> list(String catename,Integer page,Integer limit){
        PageInfoVO<CateVO2> pageInfoVO = yddCateService.list(catename,page,limit);
        return R.success(pageInfoVO);
    }

    /**
     * 分类列表-全部
     *
     * @return
     */
    @GetMapping("/all")
    public R<List<CateVO2>> allData(){
        List<CateVO2> cateVO2List = yddCateService.allData();
        return R.success(cateVO2List);
    }

    /**
     *分类添加
     *
     * @return
     */
    @PostMapping("/add")
    public R<?> save(@RequestBody CateDTO cateDTO){
        yddCateService.save(cateDTO);
        return R.success();
    }

    /**
     * 回显
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R<CateVO2> rowInfo(Integer id){
        CateVO2 cateVO2 = yddCateService.rowInfo(id);
        return R.success(cateVO2);
    }

    /**
     * 修改
     *
     * @param cateDTO
     * @return
     */
    @PostMapping("/update")
    public R<?> update(@RequestBody CateDTO cateDTO){
        yddCateService.update(cateDTO);
        return R.success();
    }

    /**
     * 分类删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R<?> delete(Integer id){
        yddCateService.delete(id);
        return R.success();
    }
}
